package com.bc004346.travelus.DataAccess.AsyncTasks;

import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;

import com.bc004346.travelus.DataAccess.TravelUsScreenScraper;
import com.bc004346.travelus.Model.Domain.Passenger;
import com.bc004346.travelus.Model.Domain.Reservation;
import com.bc004346.travelus.Model.Domain.StringList;
import com.bc004346.travelus.Model.Interface.ICompleteBookingCallback;
import com.bc004346.travelus.Utility.Utility;

import static com.bc004346.travelus.Utility.Utility.log;

/**
 * Created by Renats on 4/1/14.
 * Complete booking and get PNR info
 */
public class CompleteBookingTaskFragment extends Fragment {

    //<editor-fold desc="Members">
    private static final String PAX = "pax";
    private static final String SELECTED_PAX = "selectedPax";
    private ICompleteBookingCallback mCallbacks;
    private TravelUsScreenScraper mTravelUS;
    private Passenger mPax;
    private StringList mSelectedPassengers;
    //</editor-fold>

    //<editor-fold desc="Static Methods">
    public static CompleteBookingTaskFragment newInstance(Passenger pax,
                                                          StringList selectedPax) {
        CompleteBookingTaskFragment f = new CompleteBookingTaskFragment();
        Bundle args = new Bundle();
        args.putSerializable(PAX, pax);
        args.putSerializable(SELECTED_PAX, selectedPax);
        f.setArguments(args);
        return f;
    }
    //</editor-fold>

    //<editor-fold desc="Overrides">
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mCallbacks = (ICompleteBookingCallback) activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        mTravelUS = Utility.getInstance().getTravelUS();
        mPax = (Passenger) (getArguments() == null ? null : getArguments().getSerializable(PAX));
        mSelectedPassengers = getArguments() == null ? null : (StringList) getArguments().getSerializable(SELECTED_PAX);
        new GetPriceTask().execute();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }
    //</editor-fold>


    private class GetPriceTask extends AsyncTask<Void, Void, Reservation> {

        private Exception ex = null;

        @Override
        protected void onPostExecute(Reservation param) {
            if (mCallbacks != null) {
                mCallbacks.onCompleteBookingPostExecute(param, ex);
            }
        }

        @Override
        protected Reservation doInBackground(Void... params) {
            Reservation res = null;
            try {
                //res = new Reservation("TEST");
                res = mTravelUS.completeBooking(mPax, mSelectedPassengers);
            } catch (Exception e) {
                log(String.format("Exception: %s", e.toString()));
                ex = e;
            }
            return res;
        }
    }
}
